/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.serializer.dataser.serializer.base;

import com.inspur.edp.caf.generator.item.ClassGenerator;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.dataser.CefDataSerGenContext;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.ArrayList;

public abstract class CefNodeDataSerGenerator extends ClassGenerator {
//    protected ArrayList<Class> serTypes;
    protected CefDataSerGenContext dataSerGenContext;
    protected CefNodeDataSerGenContext parentSerGenContext;

    public CefNodeDataSerGenerator(CefNodeDataSerGenContext parentSerGenContext, CefDataSerGenContext dataSerGenContext) {
        super(dataSerGenContext.getMgrContext().getProjContext().getCompilationUnitInfo());
//        this.serTypes = serTypes;
        this.dataSerGenContext = dataSerGenContext;
        this.parentSerGenContext = parentSerGenContext;
    }

    @Override
    protected void beforeInitialize() {
        super.beforeInitialize();
    }

    private CefNodeDataSerGenContext getNodeDataSerContext() {
        return (CefNodeDataSerGenContext) super.getContext();
    }

    @Override
    protected ClassGeneratorContext createClassInfoContext() {
        return new CefNodeDataSerGenContext(dataSerGenContext);
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
        ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
        list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
        return list;
    }

    //protected override TypeInfo setParentClass()
    //{
    //    return TypeInfo.BuildBasicTypeInfo(typeof(AbstractBffEntitySerConvertor));
    //}

    //protected override List<ClassMethodGenerator> createConstructorGenerators()
    //{
    //    return new List<ClassMethodGenerator>() { new VmNodeDataCtorGenerators(getName(), isRoot, Context) };
    //}

    //protected override List<ClassMethodGenerator> createMethodGenerators()
    //{
    //    return new List<ClassMethodGenerator>()
    //    { new ConvertgetTypesGenerator(Context.SerTypes),new GetChildConvertorGenerator(Context)};
    //}

    //protected override List<BaseGenerator> createClassExtendChildGenrators()
    //{
    //    List<BaseGenerator> list = new List<BaseGenerator>();
    //    var childTypes = getChildTypes();
    //    foreach (var item in childTypes)
    //    {
    //        list.Add(new VmNodeDataSerGenerator(item.Key, false, item.Value.Key, item.Value.Value, Context, edSerGenContext));
    //    }
    //    return list;
    //}

    //private Dictionary<string, KeyValuePair<List<Type>, List<Type>>> getChildTypes()
    //{
    //    Dictionary<string, KeyValuePair<List<Type>, List<Type>>> childTypes = new Dictionary<string, KeyValuePair<List<Type>, List<Type>>>();
    //    foreach (var item in entityTypes)
    //    {
    //        foreach (var prop in item.getProperties())
    //        {
    //            var childAttributes = prop.getCustomAttributes(typeof(VMChildAttribute), false);
    //            if (childAttributes != null && childAttributes.Length != 0)
    //            {
    //                var childAttribute = childAttributes[0] as VMChildAttribute;
    //                //TODO: 应该根据nodeCode从entity上的cefData标签获取, vo集成cef后重新改.
    //                var serType = childAttribute.SerializerType;
    //                if (serType == null)
    //                {
    //                    Type implType = Context.EDSerGenContext.EntityDataGenContext.FsGenContext.MgrInterfaceAndType[0].ImplType;
    //                    serType = implType.Assembly.getType(implType.Namespace + "." + childAttribute.ObjectCode + "SerItem");
    //                }
    //                var targetInterfaceType = childAttribute.TargetInterfaceType;
    //                if (targetInterfaceType == null)
    //                {
    //                    targetInterfaceType = item.Assembly.getType($"{item.Namespace}.I{childAttribute.ObjectCode}View");
    //                }
    //                AddToChildTypes(childTypes, childAttribute.ObjectCode, serType, targetInterfaceType);
    //            }
    //            var childExtendAttribute = prop.getCustomAttributes(typeof(ChildExtendAttribute), false);
    //            //TODO: 同上
    //            if (childExtendAttribute != null && childExtendAttribute.Length != 0)
    //            {
    //                ChildExtendAttribute childExtendAttr = childExtendAttribute[0] as ChildExtendAttribute;
    //                AddToChildTypes(childTypes, childExtendAttr.ObjectCode, childExtendAttr.SerializerType, childExtendAttr.ObjectType);
    //            }
    //        }

    //    }
    //    return childTypes;
    //}

    //private void addToChildTypes(Dictionary<string, KeyValuePair<List<Type>, List<Type>>> childTypes, string childCode, Type serType, Type entityType)
    //{
    //    KeyValuePair<List<Type>, List<Type>> types;
    //    if (childTypes.ContainsKey(childCode))
    //        types = childTypes[childCode];
    //    else
    //    {
    //        types = new KeyValuePair<List<Type>, List<Type>>(new List<Type>(), new List<Type>());
    //        childTypes.Add(childCode, types);
    //    }
    //    if (serType != null)
    //        types.Key.Add(serType);
    //    if (entityType != null)
    //        types.Value.Add(entityType);
    //}
}
